library(ggplot2)
library(tidyr)
library(tidyverse)
library(patchwork)


# Figure 1: Comparison of Cumulative chart with lines -- by Theory

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_2/")

Dat3 <- read.csv("Paper_2_Data_Clean.csv", stringsAsFactors = FALSE)

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_3/")

Dat <- read.csv("Paper_3_Data_Clean.csv", stringsAsFactors = FALSE)


x =seq(0, 100, by = 0.5)


my_ecdf <- 
  . %>% 
  split(.$NuclearTheory_fac) %>% 
  map(~ecdf(.$outcome_custom)(x)) %>% 
  data.frame() %>% 
  mutate(x = x) %>% 
  pivot_longer(-x) %>% 
  mutate(
    NuclearTheory_fac = name,
    outcome_custom = value
  )


gg_df <- 
  Dat3 %>% my_ecdf()


many_bootstraps <- 
  1:100 %>%
  map(~ sample_n(Dat3, size = n(), replace = TRUE)) %>% 
  map_df(my_ecdf, .id = "sim") %>% 
  mutate(sim_group = paste0(sim, NuclearTheory_fac))



g1 <- ggplot(gg_df,
            aes(y = outcome_custom, x = x, group = NuclearTheory_fac, color = NuclearTheory_fac)) +
  geom_line(size = 1.5) +
  geom_line(data = many_bootstraps, aes(group = sim_group), size = 0.5, alpha = 0.05) +
  ggtitle("") + 
  theme_bw() +
  scale_color_manual(values = c(
    "yellow3",
    "green3",
    "orange3",
    "red3")) +
  ylab("Cumulative Proportion") + xlab("Likelihood of Success") +
  annotate(geom = "label", x = 25, y = .15, label = "Never Ever") +
  annotate(geom = "label", x = 25, y = .26, label = "Offense/Defense") +  
  annotate(geom = "label", x = 25, y = .35, label = "Existential\nDeterrence") +
  annotate(geom = "label", x = 25, y = .56, label = "Utilitarian") +
  coord_cartesian(xlim=c(0,100)) + 
  theme(plot.title = element_text(hjust = 0.5), 
        plot.subtitle = element_text(hjust = 0.5),
        legend.position = "none")+
  ggtitle("Allison 2022 Sample")




gg_df2 <- 
  Dat %>% my_ecdf()


many_bootstraps <- 
  1:100 %>%
  map(~ sample_n(Dat, size = n(), replace = TRUE)) %>% 
  map_df(my_ecdf, .id = "sim") %>% 
  mutate(sim_group = paste0(sim, NuclearTheory_fac))



g2 <- ggplot(gg_df2,
             aes(y = outcome_custom, x = x, group = NuclearTheory_fac, color = NuclearTheory_fac)) +
  geom_line(size = 1.5) +
  geom_line(data = many_bootstraps, aes(group = sim_group), size = 0.5, alpha = 0.05) +
  ggtitle("") + 
  theme_bw() +
  scale_color_manual(values = c(
    "yellow3",
    "green3",
    "orange3",
    "red3")) +
  ylab("") + xlab("Likelihood of Success") +
  annotate(geom = "label", x = 25, y = .15, label = "Never Ever") +
  annotate(geom = "label", x = 25, y = .29, label = "Offense/Defense") +  
  annotate(geom = "label", x = 25, y = .38, label = "Existential\nDeterrence") +
  annotate(geom = "label", x = 25, y = .56, label = "Utilitarian") +
  coord_cartesian(xlim=c(0,100)) + 
  theme(plot.title = element_text(hjust = 0.5), 
        plot.subtitle = element_text(hjust = 0.5),
        legend.position = "none") +
  ggtitle("Fixed or Fickle Sample")


  
g <- g2+g1  

g

ggsave("Image_1.png", plot=g, width=4000, height=2000, units=c("px"))